package com.rainng.coursesystem.manager.admin;

import com.rainng.coursesystem.dao.TeamDAO;
import com.rainng.coursesystem.dao.AreaDAO;
import com.rainng.coursesystem.dao.ActivityDAO;
import com.rainng.coursesystem.manager.BaseManager;
import com.rainng.coursesystem.model.entity.AreaEntity;
import com.rainng.coursesystem.model.entity.ActivityEntity;
import com.rainng.coursesystem.model.vo.response.IdNameVO;
import com.rainng.coursesystem.model.vo.response.table.ActivityItemVO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ActivityManager extends BaseManager {
    private final ActivityDAO activityDAO;
    private final AreaDAO areaDAO;
    private final TeamDAO teamDAO;

    public ActivityManager(ActivityDAO activityDAO, AreaDAO areaDAO, TeamDAO teamDAO) {
        this.activityDAO = activityDAO;
        this.areaDAO = areaDAO;
        this.teamDAO = teamDAO;
    }

    public Integer getPageCount(String areaName, String name) {
        int count = activityDAO.count(areaName, name);
        return calcPageCount(count, ActivityDAO.PAGE_SIZE);
    }

    public List<ActivityItemVO> getPage(Integer index, String areaName, String name) {
        return activityDAO.getPage(index, areaName, name);
    }

    public ActivityEntity get(Integer id) {
        return activityDAO.get(id);
    }

    public int create(ActivityEntity entity) {
        return activityDAO.insert(entity);
    }

    public int update(ActivityEntity entity) {
        return activityDAO.update(entity);
    }

    public int delete(Integer id) {
        return activityDAO.delete(id);
    }

    public boolean hasTeam(Integer activityId) {
        return teamDAO.countByActivityId(activityId) > 0;
    }

    public AreaEntity getAreaById(Integer id) {
        return areaDAO.get(id);
    }

    public List<IdNameVO> listName() {
        List<IdNameVO> voList = new ArrayList<>();
        List<ActivityEntity> entityList = activityDAO.listName();
        for (ActivityEntity entity : entityList) {
            voList.add(new IdNameVO(entity.getId(), entity.getName()));
        }

        return voList;
    }
}
